package com.bxd.baodan.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.activity.LoginActivity;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.item.PlanerBean.PlanerBean_;
import com.bxd.baodan.util.GlideUtil;
import com.tencent.TIMConversationType;
import com.tencent.qcloud.timchat.ui.ChatActivity;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

/**
 * Created by lizhichao on 17/7/24.
 */


public class PlanerRecycleAdapter extends RecyclerView.Adapter<PlanerRecycleAdapter.ViewHolder> {
    private Context context;
    private List<PlanerBean_> mData;

    public PlanerRecycleAdapter(List<PlanerBean_> data, Context context) {
        this.mData = data;
        this.context = context;
    }

    public void updateData(List<PlanerBean_> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        // 实例化展示的view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.question_planer_item, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        final PlanerBean_ item = mData.get(position);

        GlideUtil.loadImage((Activity)context, item.avatar_l, holder.iv);
        holder.title.setText(item.nickname);
        holder.sign.setText(item.sign);
        holder.main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (loginStatus()) {

                    MobclickAgent.onEvent(App.getContext(), "v6_1_3_BW_zx");
                    ChatActivity.navToChat(context, item.id + "", true, item, TIMConversationType.C2C);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv;
        private TextView title;
        private TextView sign;
        private LinearLayout main;


        public ViewHolder(View view) {
            super(view);
            iv = (ImageView) view.findViewById(R.id.avatar);
            title = (TextView) view.findViewById(R.id.name);
            sign = (TextView) view.findViewById(R.id.sign);
            main = (LinearLayout) view.findViewById(R.id.main);
        }
    }

    public boolean loginStatus() {

        boolean isLogin = (LoginInfo.getInstance()
                .getUser().getU_id() != null);

        if (!isLogin) {
            Intent it = new Intent(context,
                    LoginActivity.class);
            context.startActivity(it);
        }

        return isLogin;
    }
}
